#pragma once

#include<string.h>
#include<assert.h>
#include<iostream>

using namespace std;

namespace yyh
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin()const
		{
			return _str;
		}

		const_iterator end()const
		{
			return _str + _size;
		}

		void swap(string& s);

		string(const char* str = "");
		//string(const string& s);
		string(const string& s);
		string& operator =(const string& s);

		const char* c_str()const
		{
			return _str;
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		~string();
		void push_back(char ch);
		void append(const char* str);
		void reserve(size_t n);
		string& operator +=(char ch);
		string& operator +=(const char* str);
		void insert(size_t pos, size_t n, char ch);
		void insert(size_t pos, const char* str);
		void erase(size_t pos = 0, size_t len = npos);
		size_t find(char ch, size_t pos = 0);
		size_t find(const char* str, size_t pos = 0);
		size_t size()const
		{
			return _size;
		}
		size_t capacity()const
		{
			return _size;
		}
		char& operator [](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}
		const char& operator [](size_t pos)const
		{
			assert(pos < _size);
			return _str[pos];
		}
		string substr(size_t pos, size_t len = npos);
		bool operator ==(const string& s) const;
		bool operator !=(const string& s) const;
		bool operator <(const string& s) const;
		bool operator <=(const string& s) const;
		bool operator >(const string& s) const;
		bool operator >=(const string& s) const;
	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;
		const static size_t npos;
	};

	ostream& operator <<(ostream& out, const string& s);
	istream& operator >>(istream& in, string& s);
	istream& getline(istream& is, string& s, char delim = '\n');

	void test1();
	void test2();
	void test3();
	void test4();
	void test5();
	void test6();
}